#fileProcessing.py - Handles the file input and file output tasks.
import csv
import shutil
import datetime

class fileProcessing(object):

    def __init__(self):
        """Sets roster and quizname."""
        pass

    def findNumOfStudents(self,rosterFile):
        """Finds number of students in roster."""
        self.csvfile=open(rosterFile,'rb')
        self.reader=csv.reader(self.csvfile)
        self.numOfStudents=-1
        for row in self.reader:
            self.numOfStudents=self.numOfStudents+1

    def writeResultFile(self,rosterFile,responses,numOfQuestions):
        """Writes a csv file of the responses.
        
        Inserts *'s for people that did not respond."""
        self.quizName=datetime.datetime.now().strftime("%m.%d.%Y.at.%H.%M")
        shutil.copy(rosterFile,'responses.'+self.quizName+'.csv')

        self.csvfile=open(rosterFile,'rb')
        self.csvout=open('responses.'+self.quizName+'.csv','wb')

        self.reader=csv.reader(self.csvfile)
        self.writer=csv.writer(self.csvout)

        for rowNum,row in enumerate(self.reader):
            if rowNum==0:
                pass
            else:
                if row[0] in responses:
                    for ans in responses[row[0]]:
                        row.append(ans)
                else:
                    for dumAns in range(numOfQuestions):
                        row.append('*')
            self.writer.writerow(row)

        self.csvfile.close()
        self.csvout.close()

    def getUser(self,confFile):
        return open(confFile).readline()

    def gradeQuiz(self,responsesFile,answerKey,pointsRightAnswer,pointsWrongAnswer,pointsNoAnswer,totalPointsStr):
        """Grades response file."""
        self.gradeFile=responsesFile.replace('responses','grades')
        shutil.copy(responsesFile,self.gradeFile)

        self.csvfile=open(responsesFile,'rb')
        self.csvout=open(self.gradeFile,'wb')

        self.reader=csv.reader(self.csvfile)
        self.writer=csv.writer(self.csvout)

        for rowNum,row in enumerate(self.reader):
            self.points=0
            if rowNum==0:
                self.quizName=responsesFile.replace('responses','quiz').strip('.csv')
                self.quizNameHeader=self.quizName + ' [' + totalPointsStr + ']'
                row.append(self.quizNameHeader)
            else:
                for answer in answerKey:
                    if row[3] == answer:
                        self.points=self.points+pointsRightAnswer
                    elif row[3] == '*':
                        self.points=self.points+pointsNoAnswer
                    else:
                        self.points=self.points+pointsWrongAnswer
                    row.pop(3)
                row[0]=''
                row.append(self.points)
            self.writer.writerow(row)
            
        self.csvfile.close()
        self.csvout.close()

    def getNumOfQuestions(self,responsesFile):
        """Finds number of questions in response file. Returns result."""
        self.csvfile=open(responsesFile,'rb')
        self.reader=csv.reader(self.csvfile)
        self.numOfQuestions=0

        for rowNum,row in enumerate(self.reader):
            if rowNum==0:
                pass
            elif rowNum==1:
                self.numOfQuestions=len(row)-3
            else:
                pass

        self.csvfile.close()
        return self.numOfQuestions
        
